import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { User } from './user.entity';
import { UserService } from './user.service';


import {
  apibefore
} from '../../router'
import utils from 'src/common/utils';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';
@Controller(apibefore('/sysuser'))
export class UserController {
  constructor(private readonly service: UserService) {}

  /**
   * 登录
   * @param data
   * @returns
   */
  @Post('/login_validauth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async login_validauth(@Body() data: User){
    let password = utils.md5(data.password)
    let user = await this.service.login(data.username, password)
    if(!user){
      return new HttpException('登录失败，账号或密码错误。', HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return {id:user.id,username:user.username,wx_user: user.wx_user}
    }
  }


  /**
   * 登录
   * @param data
   * @returns
   */
  @Post('/login')
  async login(@Body() data: User){
    let password = utils.md5(data.password)
    let user = await this.service.login(data.username, password)
    if(!user){
      return new HttpException('登录失败，账号或密码错误。', HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return {id:user.id,username:user.username,wx_user: user.wx_user}
    }
  }

  @Get('/info')
  async info(@Query() data: User){
    let user = await this.service.findOne(data.id)
    return {
      roles:user.role.split(','),
      name:user.name
    }
  }
}
